package com.dropgame.screens;

import java.io.IOException;
import java.io.StringWriter;

import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.TweenManager;
import aurelienribon.tweenengine.equations.Bounce;
import aurelienribon.tweenengine.equations.Elastic;

import com.dropgame.main.*;
import com.dropgame.tools.ImageButton;
import com.dropgame.tools.ImageButtonAccessor;
import com.dropgame.tools.InvertedInput;
import com.dropgame.tools.SpriteButton;
import com.dropgame.tools.SpriteButtonAccessor;
import com.dropgame.tools.ToastTest;
import com.dropgame.tools.TransitionEffect;
import com.dropgame.tools.ImageButton2;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.GL11;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonWriter;


public class MainMenuScreen implements Screen{

	
	private DropGame game;
	private OrthographicCamera guiCam;
	
	private static final int VIRTUAL_WIDTH = 320;
    private static final int VIRTUAL_HEIGHT = 480;
    private static final float ASPECT_RATIO = (float)VIRTUAL_WIDTH/(float)VIRTUAL_HEIGHT;
    
    public Rectangle viewport;
	private TransitionEffect transitionEffect;

	
	private TextureRegion start;
	
	private SpriteBatch batcher;
	
	
	//private Animation start1;
	private float plusDelta;
	
	private String title;
	
	private String name;
	
	private Sprite savanah;
	
	private Input input;
	
	private ImageButton startButton;
	
	private TweenManager manager;
	
	private Tween sBTween;
	
	BitmapFont font;
	
	public boolean levelTest;
	
	//ToastTest toast;
	
	public MainMenuScreen(DropGame game){	
		this.game = game;
		
		// Create the camera with the virtual width and height.
        this.guiCam = new OrthographicCamera(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
        
        //Set the camera to point at the center of the screen.
		guiCam.position.set(VIRTUAL_WIDTH / 2, VIRTUAL_HEIGHT / 2, 0);
		
		// Create viewport to be used in resizing etc.
		// The viewport is initially set to 0.0f but will be resized in resize() when screen is created.
		// This is done to prevent null pointer exceptions happening in various classes that use the viewport.
		this.viewport = new Rectangle(0.0f,0.0f,0.0f,0.0f);
		
		// Create the input.
		this.input = Gdx.input;
		
		// This was an animation test 
		//this.start1 = AssetLoader.anamStart;
		
		// Load the back ground from the assetLoader.
		this.savanah = AssetLoader.savanah;
		
		
		this.start = AssetLoader.startRegion;
		
		// Create the SpriteBatch that will be used to draw our assets.
		this.batcher = new SpriteBatch();
		
		// Create the transitionEffect that is used to display an fade in and out effect when changing screens.
		this.transitionEffect = new TransitionEffect(game);
		
		//  Create the tween manager.
		this.manager = new TweenManager();
		this.title = "Drops";
		font = new BitmapFont();
		
		//this.name = game.getProfileService().retrieveProfile().name;
		//font = TrueTypeFontFactory.createBitmapFont(Gdx.files.internal("data/font/KRONIKA_.ttf"), FONT_CHARACTERS, 12.5f, 7.5f, 1.0f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

		// Create a callback for when the tween for the Start button finishes and the button can accept input. 
		TweenCallback windCallback = new TweenCallback(){

			@Override
			public void onEvent(int arg0, BaseTween<?> arg1) {
				Gdx.app.log("MainMenuScreen", "The callback happened");
				startButton.setAcceptInput(true);
			}
			
		};
		
		// Create an array of sprite's for the Start button.
		Sprite[] startArray= {AssetLoader.startButton1, AssetLoader.startButton2, AssetLoader.startButton3, AssetLoader.startButton4, AssetLoader.startButton5};
		
		// Create a new ImageButton for moving on to the next screen.
		this.startButton = new ImageButton(0.15f, startArray, input, guiCam, viewport, 95.0f, 100.0f);
		
		// Set the button to not accept input until the tween is finished.
		startButton.setAcceptInput(false);

		Tween.registerAccessor(ImageButton.class, new ImageButtonAccessor());
		Tween.set(startButton, ImageButtonAccessor.POSITION_XY).target(95.0f, 500.0f).start(manager);
		Tween.to(startButton, ImageButtonAccessor.POSITION_XY, 0.5f).target(95.0f, 100.0f).setCallback(windCallback).start(manager);
		
		
		
		Preferences prefs = Gdx.app.getPreferences("MyPreferences");
		prefs.putString("name", "Brian");
		prefs.putString("name", "Jones");
		String getName = prefs.getString("name");
		
		//toast = new ToastTest(210, 25);
		//toast.makeText("Game start", "font", ToastTest.COLOR_PREF.BLUE, ToastTest.STYLE.ROUND, ToastTest.TEXT_POS.middle_right, ToastTest.TEXT_POS.middle_down, ToastTest.ETERNAL);
	}
	
	@Override
	public void dispose() {
		
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void render(float deltaTime) {
		
		// Create an instance of the graphics.
		GL10 gl = Gdx.app.getGraphics().getGL10();
		
		guiCam.update();
		guiCam.apply(gl);
		
		
		gl.glViewport((int) viewport.x, (int) viewport.y,
                	  (int) viewport.width, (int) viewport.height);
		
		// Clear the screen for the next frame to be drawn.
		gl.glClearColor(0, 0, 0, 0);
		gl.glClear(GL11.GL_COLOR_BUFFER_BIT);
		
		batcher.setProjectionMatrix(guiCam.combined);
		batcher.begin();
		
		savanah.draw(batcher);
		
		Gdx.app.log("MainMenuScreen", " " + game.multipleProfile.returnDefault().levels[0].playable);
		plusDelta += Gdx.graphics.getDeltaTime();
		TextureRegion keyFrame = AssetLoader.anamStart.getKeyFrame(plusDelta, true);
		//batcher.draw(keyFrame, 0, 400);
		//batcher.draw(startButton2.out, startButton2.xPos, startButton2.yPos);	
		
		
		startButton.getKeyFrame(deltaTime).draw(batcher);
		//playButton.getKeyFrame(deltaTime).draw(batcher);
		//thirdButton.getKeyFrame(deltaTime).draw(batcher);
		font.draw(batcher, title , 10.0f, 300.0f);
		font.draw(batcher, "Hello " + game.multipleProfile.returnDefault().name, 25.0f, 25.0f);
		batcher.end();
		transitionEffect.movingScreen(batcher, deltaTime);
		
		
		updateLogic(deltaTime);
		
		
	}
	
	public void updateLogic(float deltaTime){
		
		// If the start button is completed transition screen.
		if(startButton.getCompleted()){
			transitionEffect.nextScreen(new SecondMenuScreenTE(game));
			
		}
		
		// Update the tween manager.
		manager.update(deltaTime);
	}
	
	
	
	

	@Override
	public void resize(int width, int height) {
		float aspectRatio = (float)width/(float)height;
        float scale = 1f;
        Vector2 crop = new Vector2(0f, 0f);
        if(aspectRatio > ASPECT_RATIO)
        {
            scale = (float)height/(float)VIRTUAL_HEIGHT;
            crop.x = (width - VIRTUAL_WIDTH*scale)/2f;
        }
        else if(aspectRatio < ASPECT_RATIO)
        {
            scale = (float)width/(float)VIRTUAL_WIDTH;
            crop.y = (height - VIRTUAL_HEIGHT*scale)/2f;
        }
        else
        {
            scale = (float)width/(float)VIRTUAL_WIDTH;
        }

        float w = (float)VIRTUAL_WIDTH*scale;
        float h = (float)VIRTUAL_HEIGHT*scale;
        viewport.set(crop.x, crop.y, w, h);
        
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
	}
	
	public void isEnteringScreen(float deltaTime){
		
	}

}
